// 引进 axios
import axios from 'axios'

// 配置基础路径
axios.defaults.baseURL = 'http://localhost:8888/api/private/v1/'

// 添加axios请求拦截器
axios.interceptors.request.use(function (config) {
  // 将token给到一个前后台约定好的key中，作为请求发送
  let token = localStorage.getItem('mytoken')
  if (token) {
    // 若token存在，设置请求头
    config.headers['Authorization'] = token
  }
  return config
}, function (error) {
  // Do something with request error
  return Promise.reject(error)
})

/** 登录请求验证 **/
// export const getLogin = paramsObj => { return axios.post('/login', paramsObj) }
export const getLogin = paramsObj => axios.post('/login', paramsObj)

/** 获取页面左侧权限管理分类列表 **/
export const getMenus = () => axios.get('/menus').then(res => res.data)

/* *** 用户管理 *** */

/* 获取用户数据列表 */
// `params` 是即将与请求一起发送的 URL 参数
// 必须是一个无格式对象(plain object)或 URLSearchParams 对象
export const getUserList = paramsObj => axios.get('/users', { params: paramsObj }).then(res => res.data)

/* 添加用户 */
export const addUser = paramsObj => axios.post('/users', paramsObj).then(res => res.data)

/* 编辑用户 */
export const editUser = (id, paramsObj) => axios.put(`/users/${id}`, paramsObj).then(res => res.data)

/* 修改用户状态 */
export const modifyUserStyle = (id, type) => axios.put(`/users/${id}/state/${type}`).then(res => res.data)

/* 删除单个用户 */
export const deleteUser = id => axios.delete(`/users/${id}`).then(res => res.data)

/* 分配用户角色 */
export const assignRoles = (id, rid) => axios.put(`/users/${id}/role`, { rid: rid }).then(res => res.data)

/** *** 权限管理 *** **/

/* 获取分配角色的类型列表 */
export const getRoles = () => axios.get('/roles').then(res => res.data)

/* 添加角色 */
export const addRole = (roleName, roleDesc) => axios.post('/roles', { roleName: roleName, roleDesc: roleDesc }).then(res => res.data)

/* 编辑角色 */
export const editRoles = (id, obj) => axios.put(`/roles/${id}`, obj).then(res => res.data)

/* 删除某角色类 */
export const deleteRole = id => axios.delete(`/roles/${id}`).then(res => res.data)

/* 删除角色指定权限 */
export const deleteRolePower = (roleId, rightId) => axios.delete(`/roles/${roleId}/rights/${rightId}`).then(res => res.data)

/* 角色授权 */
export const addRolePower = (roleId, rids) => axios.post(`/roles/${roleId}/rights`, { rids: rids }).then(res => res.data)

/** 所有权限列表 **/
export const getRights = type => axios.get(`/rights/${type}`).then(res => res.data)

/** 商品管理  **/
// 商品分类列表
export const getCategories = (type, pagesize, pagenum) => axios.get('/categories', { params: { type: type, pagesize: pagesize, pagenum: pagenum } }).then(res => res.data)

// 添加商品分类
export const addCategories = cateObj => axios.post('/categories', cateObj).then(res => res.data)

// 删除商品分类
export const deleteCate = id => axios.delete(`/categories/${id}`).then(res => res.data)

// 根据id查询分类
export const queryCate = id => axios.get(`/categories/${id}`).then(res => res.data)

// 编辑商品分类
export const editCate = (id, obj) => axios.put(`categories/${id}`, obj).then(res => res.data)
